#include "FormationBody.h"
#include <iostream>
using namespace std;

FormationBody::FormationBody(FormationState state) {
    this->state = state;
    this->leaderSpot = NULL;
    this->spots = new vector<FormationSlot*>();
}

FormationBody::~FormationBody() {
    if (!leaderSpot) delete leaderSpot;
    if (!spots) {
        spots->clear();
        delete spots;
    }
}

FormationBody::FormationBody(FormationBody& fb) {
    this->leaderSpot = fb.leaderSpot;
    this->spots = fb.spots;
}

FormationBody& FormationBody::operator=(FormationBody& fb) {
    this->clear();
    this->leaderSpot = fb.leaderSpot;
    this->spots = fb.spots;
    return *this;
}

void FormationBody::clear() {
    this->leaderSpot = NULL;
    this->spots->clear();
}

void FormationBody::addSpot(FormationSlot* spot) {
    this->spots->push_back(spot);
    if (!(*spot).getSpotParent()) this->leaderSpot = spot;
}

vector<FormationSlot*> FormationBody::getSlots() {
    return *(this->spots);
}

FormationSlot& FormationBody::getSlotAt(int index) {
    if ((index < 0) || (index > this->spots->size())) throw string("Index Out Of Range exception");
    return (*(*this->spots)[index]);
}

void FormationBody::scale(int spotCount) {
    clear();
    for (int i = 0; i < spotCount; i++)
        createSpot(i, spotCount);
}

void FormationBody::setGlobalPosition(double x, double y) {
    if (this->leaderSpot) this->leaderSpot->setGlobalPosition(x,y);
}

Point2D FormationBody::getGlobalPosition() {
    if (this->leaderSpot) return this->leaderSpot->getGlobalPosition();
    else return Point2D(0,0);
}

void FormationBody::setGlobalOrientation(double x, double y) {
    if (this->leaderSpot) this->leaderSpot->setGlobalOrientation(x,y);
}

FormationState FormationBody::getState() {
    return state;
}
